/**
 * 清空 .titleSearch（默认） 的筛选项
 */
function clearSearchKeys(selector) {
    if (selector == undefined) {
        selector = ".titleSearch";
    }
    $(selector).clearForm();
    $(selector).find('select').val(null).trigger("change");
}

/**
 * 刷新表格
 */
function refreshDataTable(tableId) {
    if (tableId == undefined) {
        tableId = "myTable";
    }
    $("#" + tableId).dataTableExtend("reloadFalse");
}


/**
 * 弹出更新工单弹窗
 */
function OpenWorkOrderUpdateLayer(tableId) {
    if (tableId == undefined) {
        tableId = "myTable";
    }
    var datas = $("#" + tableId).dataTableExtend('getCheck');
    if (datas.length > 1) {
        DGG.Layer.message("请逐个更新工单!", {icon: 2});
        return;
    } else if (datas.length < 1) {
        DGG.Layer.message("请选择一个工单进行更新操作!", {icon: 0});
        return;
    }
    // debugger;
    var workOrderId = datas[0].id;
    var url = "/sc/general/workOrder/update.html?id=" + workOrderId;
    DGG.Layer.open(url, {
        title: "工单更新",
        area: ["50%", "90%"],
        btn: "",
        cancel: function (index, obj) {
            $("#" + tableId).dataTableExtend("reloadFalse");
        }

    });

}

/**
 * 弹出生产工单详情页面
 */
function openWorkOrderDetail(id, from) {
    if (undefined == id) {
        var parent = $(event.target).parents("tr").eq(0);
        id = parent.find("input[name='id']").val();
    }
    var url = "/sc/general/workOrder/detail.html?id=" + id;
    if (from == undefined) {
        from = this.window;
    } else if (from == "parent") {
        from = this.parent.window;
    }
    from.DGG.Layer.open(url, {
        title: "工单详情",
        area: ["50%", "90%"],
        btn: "",
        cancel: function (index, obj) {

        }
    });
}

/**
 * 弹出新建工单页面,根据传入参数,初始化新建页面的一些数据(生产订单编号,客户信息)
 * 默认在id为myTable的表格中查找数据
 */
function newWorkOrderFromTable(tableId) {
    if (tableId == undefined) {
        tableId = "myTable";
    }
    //  debugger;
    var datas = $("#" + tableId).dataTableExtend("getCheck");

    var type, id, entity;
    if (datas.length > 0) {
        var data = datas[0];
        id = data.id;
        if ('scWorkorderNo' in data) {
            type = "workOrder";
            entity = "工单";
        }
        else if ('scProductOrderNo' in data) {
            type = "scSubOrder";
            entity = "生产子订单";
        }
    }
    if (datas.length > 1) {
        DGG.Layer.message("仅能选择一条" + entity + "!", {icon: 0});
        return;
    }
    newWorkOrder(id, type);

}

/**
 * 新建工单
 */
function newWorkOrder(id, type) {
    var para = "";
    if (type == undefined) type = "scSubOrder";
    if (id != undefined) {
        para = "?type=" + type + "&id=" + id;
    }
    DGG.Layer.open(sysInfo.basePath + "/sc/general/workOrder/new.html" + para, {
        title: "新建工单",
        area: ["45%", "90%"],
        btn: "",
        id: "layer-new-order",
        success: function (selector, index) {
        }
    });


}

/**
 * 重新加载表格js 內存中的table数据,重置start 和limit参数
 * id为myTable的dataTable
 */
function dataTableReload() {
    table.ajax.reload();
}

/**
 * 刷新表格页面数据
 */
function dataTableReFresh() {
    $('#myTable', document).dataTableExtend("reloadFalse");
}


/**
 * 清空表单元素(包括不可见的),忽略具有fixed属性的
 * @param selector
 */
$.fn.clearForm = function () {
    $(this).find("input,select,textarea").each(function (i, e) {
        e = $(e);
        var fixed = e.attr("fixed");
        if (fixed == undefined) {
            e.val("");
        }
    });
}

/**
 *快速筛选tab效果
 * @param option
 */
$.fn.activeTab = function (option) {
    $(this).on("click", "a", function () {
        var a = $(this);
        if (a.hasClass("active")) return;
        a.parent().find("a").removeClass("active");
        a.addClass("active");
        if (option != undefined && option.fn != undefined) {
            option.fn(this);
        }

    });
}

/**
 * 将表单元素序列化成一个属性对象
 * @param options 可用序列化参数：
 * {
     * allowEmpty:false,
     * includeHidden:true,
     * trimString:true
     * }
 * @return object
 */
$.fn.serialize2object = function (options) {
    var setting = {};
    setting.allowEmpty = false;//序列化空值
    setting.includeHidden = true;//序列化隐藏值
    setting.trimString = true;//trim值
    setting.extra = undefined;//额外序列化标签
    setting.base = "input,select,textarea";//序列化标签
    $.extend(setting, options);
    var data = {};
    var tags = setting.base;
    if (setting.extra != undefined) {
        tags = tags + "," + setting.extra;
    }
    var items = $(this).find(tags);
    if (items.size() == 0) {
        console.log("no filed found in this container");
        return data;
    }
    items.each(function (i, e) {
        var item = $(e);
        var hidden = item.css("display") == "none";
        var name = item.attr("name");
        //没有name属性不序列化
        if (name == undefined || name == "") return;
        var baseTags = setting.base.split(",");
        var isBase = false;
        for (var i = 0; i < baseTags.length; i++) {
            if (item.is(baseTags[i])) {
                isBase = true;
                break;
            }
        }
        var value;
        if (isBase) {
            value = item.val();
        }
        else {
            value = item.text();
        }
        //配置要求隐藏元素不序列化
        if (hidden && !setting.includeHidden) return;
        //控件值异常不序列化
        if (value == undefined) return;
        value = value.trim();
        //配置要求空值不序列化
        if (value == "" && setting.allowEmpty == false) return;
        data[name] = value;
    });
    for (var key in data) {
        var value = data[key];
        //去掉字符串前后的空格
        if (setting.trimString) {
            value = typeof(value) == "string" ? value.trim() : value;
            data[key] = value;
        }
        value = data[key];
        //是否允许空值
        if (!setting.allowEmpty && value == "") {
            delete data[key];
        }
    }
    return data;
}
/**
 * 加载<select>标签的option，根据code属性传入字典code
 */
$.fn.loadSelectOptions = function (option) {
    var setting = {};
    setting.codeLabel = "code";
    setting.after = undefined;
    setting.type = "append";
    $.extend(setting, option);
    var url = "/sc/general/com/getSelectOption.do";
    $(this).each(function (i, e) {
        var $select = $(this);
        var code = $select.attr(setting.codeLabel);
        if (code == undefined || code == "") return;
        $.ajax({
            url: url,
            type: "POST",
            data: {code: code},
            success: function (data) {
                if (setting.type == "replace") {
                    $select.empty();
                }
                $select.append(data);
                if (setting.after != undefined) {
                    setting.after($select);

                }
            }
        });
    });

}




